﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using eBook.Core.BusinessObjects;
using System.Diagnostics;

namespace eBook.WinApp
{
  public partial class ctrlBookInfo : UserControl
  {
    private boBookInfo _dataItem;
    public boBookInfo DataItem 
    {
      get 
      { 
        return _dataItem; 
      }

      set
      {
        _dataItem = value;
        
        BindDetailControls(_dataItem, this);
      }
    }

    public ctrlBookInfo()
    {
      InitializeComponent();
    }


    public void Clear()
    {
      imgBook.Image = null;
      var boxes = this.Controls.OfType<TextBox>();
      foreach (var box in boxes)
        box.Clear();
    }

    public TextBox TextBoxByName(string boxName)
    { 
      return this.Controls.Find(boxName, true).FirstOrDefault() as TextBox;      
    }

    public void CopyFromExample(boBookInfo example)
    {
      if ( example == null )
        return;

      if ( this.DataItem == null )
      {
        this.DataItem = example;
        return;
      }

      List<string> diff = new List<string>();
      PropertyInfo[] props = typeof(boBookInfo).GetProperties();
      foreach ( var p in props )
      {
        var oldValue = p.GetValue(this.DataItem, null);
        var newValue = p.GetValue(example, null);

        if ( newValue != null && ( oldValue == null || oldValue.ToString() == string.Empty ) )
        {
          if ( p.CanWrite )
            p.SetValue(this.DataItem, newValue, null);
        }
        

        if ( oldValue is string[] && newValue != null)
        {
          diff.Clear();
          string[] oldArray = oldValue as string[];
          string[] newArray = newValue as string[];
          foreach ( var item in newArray )          
            if ( !oldArray.Contains(item) )
              diff.Add(item);
          
          diff.AddRange(oldArray);
          p.SetValue(this.DataItem, diff.ToArray(), null);
        }
      }

      this.RefreshBindings();
    }

    public void RefreshBindings()
    {
      this.BindDetailControls(this.DataItem, this);
    }

    private void BindDetailControls(boBookInfo item, Control container)
    {
      if (item != null && !string.IsNullOrEmpty(item.SmallImageURL))
        imgBook.LoadAsync(item.SmallImageURL);

      PropertyInfo[] props = typeof(boBookInfo).GetProperties();
      foreach (var p in props)
      {
        Control c = container.Controls.Find("txt" + p.Name, false).FirstOrDefault();
        if (c == null)
          continue;

        c.DataBindings.Clear();
        if ( p.PropertyType.IsArray )
        {
          string[] propValue = p.GetValue(item, null) as string[];
          if ( propValue != null && propValue.Length > 0 )
            ( c as TextBox ).Lines = propValue;
        }
        else
          c.DataBindings.Add(new Binding("Text", item, p.Name));
      }
    }



  }
}
